﻿using System;
using System.Windows.Forms;
using Kay.LazarToDo.Win.AppServices;

namespace Kay.LazarToDo.Win._Forms
{
    /// <summary>
    /// 添加项目
    /// </summary>
    public partial class AddProject : Form
    {
        private readonly ProjectAppService _projectAppService;
        /// <summary>
        /// 初始化添加项目
        /// </summary>
        public AddProject()
        {
            InitializeComponent();
            _projectAppService = new ProjectAppService();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            errorProvider.Clear();
            var projectName = textBoxName.Text.Trim();
            if (string.IsNullOrWhiteSpace(projectName))
            {
                errorProvider.SetError(textBoxName, "请填写项目名称！");
                return;
            }
            if (_projectAppService.CheckName(projectName))
            {
                errorProvider.SetError(textBoxName, "项目名称已存在！");
                return;
            }
            var project = _projectAppService.SaveProject(projectName);
            if (project != null)
            {
                DialogResult = DialogResult.OK;
            }
        }
    }
}
